package com.ruoyi.project.record.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.project.maintain.domain.ProjectMaintain;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.project.record.domain.bo.ProjectBo;
import com.ruoyi.project.record.domain.vo.ProjectVo;
import com.ruoyi.project.record.domain.Project;
import com.ruoyi.project.record.mapper.ProjectMapper;
import com.ruoyi.project.record.service.IProjectService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工程记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-09
 */
@RequiredArgsConstructor
@Service
public class ProjectServiceImpl implements IProjectService {

    private final ProjectMapper baseMapper;

    /**
     * 查询工程记录
     */
    @Override
    public ProjectVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工程记录列表
     */
    @Override
    public TableDataInfo<ProjectVo> queryPageList(ProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Project> lqw = buildQueryWrapper(bo);
        Page<ProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工程记录列表
     */
    @Override
    public List<ProjectVo> queryList(ProjectBo bo) {
        LambdaQueryWrapper<Project> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Project> buildQueryWrapper(ProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Project> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), Project::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getLxr()), Project::getLxr, bo.getLxr());
        lqw.eq(StringUtils.isNotBlank(bo.getLxdh()), Project::getLxdh, bo.getLxdh());
        lqw.eq(StringUtils.isNotBlank(bo.getSgdz()), Project::getSgdz, bo.getSgdz());
        lqw.eq(StringUtils.isNotBlank(bo.getGj()), Project::getGj, bo.getGj());
        lqw.eq(StringUtils.isNotBlank(bo.getCz()), Project::getCz, bo.getCz());
        lqw.eq(bo.getCd() != null, Project::getCd, bo.getCd());
        lqw.eq(StringUtils.isNotBlank(bo.getBg()), Project::getBg, bo.getBg());
        lqw.eq(StringUtils.isNotBlank(bo.getYjgq()), Project::getYjgq, bo.getYjgq());
        lqw.eq(bo.getStarttime() != null, Project::getStarttime, bo.getStarttime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateuserid()), Project::getCreateuserid, bo.getCreateuserid());
        lqw.eq(bo.getCreateTime() != null, Project::getCreatetime, bo.getCreateTime());
        return lqw;
    }

    /**
     * 新增工程记录
     */
    @Override
    public Project insertByBo(ProjectBo bo) {
        Project add = BeanUtil.toBean(bo, Project.class);
        validEntityBeforeSave(add);
        add.setCreatetime(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add;
    }

    /**
     * 修改工程记录
     */
    @Override
    public Project updateByBo(ProjectBo bo) {
        Project update = BeanUtil.toBean(bo, Project.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return update;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Project entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工程记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
